package magicloan.auctionpocket.ui.base;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.utils.module.StringUtils;

import org.json.JSONException;
import org.json.JSONObject;

import magicloan.auctionpocket.R;
import magicloan.auctionpocket.bean.UserData;
import magicloan.auctionpocket.net.NetService;
import magicloan.auctionpocket.net.OnNetCallBack;

/**
 * Created by hechao on 2018/1/25.
 */
public abstract class BaseActivity extends FragmentActivity implements View.OnClickListener , OnNetCallBack {

    protected View mContentView ;
    protected NetService mService ;
    protected Context mContext;
    protected Activity mActivity ;
    protected UserData instance ;
    protected Gson gson;
    protected boolean isLoadMore;
    protected int mCurrentPage = 1 ;

    protected abstract int setContentView();

    protected abstract void beforeInitView();

    protected abstract void afterInitView();

    protected abstract void initData();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setWindow(true);
        mContext = mActivity = this ;
//        initWindow(isFullScreen());
        mService = new NetService(this,mActivity);
        gson = new Gson();
        instance = UserData.getInstance() ;
        beforeInitView();
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        mContentView = View.inflate(this,setContentView(),null);
        initTitle();
        setContentView(mContentView);
        initView();
        afterInitView();
        initData();

    }

    protected abstract void initView();

    protected boolean isFullScreen() {
        return true;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.img_public_left:
                finish();
                break;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    protected TextView tv_public_title ;
    protected TextView tv_public_right ;
    protected ImageView img_public_left ;
    protected void initTitle(){
        tv_public_title = (TextView) mContentView.findViewById(R.id.tv_public_title);
        tv_public_right = (TextView) mContentView.findViewById(R.id.tv_public_right);
        img_public_left = (ImageView) mContentView.findViewById(R.id.img_public_left);
    }
    protected void setTitle(String title,String right){
        tv_public_title.setText(title);
        if ( img_public_left != null )
            img_public_left.setOnClickListener(this);
        if ( tv_public_right != null && !StringUtils.isEmpty(right) ){
            tv_public_right.setText(right);
            tv_public_right.setOnClickListener(this);
            tv_public_right.setVisibility(View.VISIBLE);
        }
    }

    @TargetApi(19)
    protected void initWindow(boolean isTransalact) {
        if ( isTransalact && Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT ) {
            requestWindowFeature(Window.FEATURE_NO_TITLE);
            //全屏
//            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
//            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        }
    }

    public void afterConnectSuccess(String flag, JSONObject request) throws JSONException {

    }

    public void afterConnectFail(String flag, JSONObject result,String cache) throws JSONException {

    }
    protected SwipeRefreshLayout swipeRefreshLayout;
//    protected void initSwipeRefresh(){
//        //-------------------------- SwipeRefreshLayout
//        swipeRefreshLayout = (SwipeRefreshLayout)mContentView.findViewById(R.id.swipe_container);
//        //设置刷新时动画的颜色，可以设置4个
//        swipeRefreshLayout.setColorSchemeResources(android.R.color.holo_blue_light, android.R.color.holo_red_light, android.R.color.holo_orange_light, android.R.color.holo_green_light);
//        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
//
//            @Override
//            public void onRefresh() {
//                // TODO Auto-generated method stub
//                isLoadMore = false ;
//                mCurrentPage = 1 ;
//                initData();
//            }
//        });
//    }


    protected void setWindow(boolean isTransalact) {
        if ( isTransalact && Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT ) {
            requestWindowFeature(Window.FEATURE_NO_TITLE);
            //全屏
//            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
//            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        }
    }
}